﻿/**
 * @file		dd_pmc_tmr32.h
 * @brief		PMC 32 bit reload timer driver
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

/** @weakgroup dd_pmc_tmr32_overview
@{
	CM0 PMC - 32bit Reload Timer Driver.<br>
@}*//* --- end of dd_pmc_tmr32_overview */

/** @weakgroup dd_pmc_tmr32_sequence
@{
	- @ref dd_pmc_tmr32_sequence_sect
	- @ref dd_pmc_tmr32_sample_sect
	<br>
	@section dd_pmc_tmr32_sequence_sect		Basic sequence of 32 bit Timer
	@image html dd_pmc_tmr32_base.png
	<br>
	@section dd_pmc_tmr32_sample_sect		Sample code
	@code
	INT32 Timer32_Sample_Code(VOID)
	{
		T_PMC_TMR_CTRL 	tmr32_ctrl;
		
		tmr32_ctrl.csr.bit.rels       = 0;
		tmr32_ctrl.csr.bit.csl        = 2;		// 32div
		tmr32_ctrl.csr.bit.mod        = 0;
		tmr32_ctrl.csr.bit.outl       = 0;
		tmr32_ctrl.csr.bit.reld       = 1;		// Reload
		tmr32_ctrl.csr.bit.inte       = 1;		// Interrupt enable
		tmr32_ctrl.csr.bit.uf_fixed1  = 0;
		tmr32_ctrl.csr.bit.cnte       = 1;		// Timer counter enable
		tmr32_ctrl.csr.bit.trg_fixed0 = 0;
		
		tmr32_ctrl.rlr1         = (1687500 - 1);		// Timer Count (1sec)
		tmr32_ctrl.rlr2         = 0;
		tmr32_ctrl.soft_counter = 1;					// Soft counter
		tmr32_ctrl.pCallBack    = Sample_Code_CB;		// User's Callback Function

		ret = Dd_PMC_TMR32_Ctrl(&tmr32_ctrl);
		if (ret != D_DDIM_OK) {
			// ERROR
			return ;
		}
		ret = Dd_PMC_TMR32_Start();
		if (ret != D_DDIM_OK) {
			// ERROR
			return ;
		}
	}
	@endcode

@}*//* --- end of dd_pmc_tmr32_sequence */

#ifdef CO_DDIM_UTILITY_USE
/** @weakgroup dd_pmc_tmr32_sequence
@{
	<hr>
	Utility Functions <br>
	- @ref Timer32_start_utility_OneShot <br>
	- @ref Timer32_start_utility_Reload  <br>

	<hr>
	@section Timer32_start_utility_OneShot	Sequence of the One-Shot of 32 bit Timer. (Utility)
	@image html dd_pmc_tmr32_utility_OneShot.png

	@section Timer32_start_utility_Reload	Sequence of the Reload of 32 bit Timer. (Utility)
	@image html dd_pmc_tmr32_utility_Reload.png

	@code
	INT32 Timer32_Utility_Reload_Sample_Code(VOID)
	{
		ULONG		time;
		VP_CALLBACK	call_back;

		// Parameter Setting
		time      = 1000000;			// 1 sec
		call_back = Sample_Code_CB;		// User's Callback Function

		ret = Dd_PMC_TMR32_SetTimer(time, call_back);
		if (ret != D_DDIM_OK) {
			// ERROR
			return ;
		}
		ret = Dd_PMC_TMR32_StartReload();
		if (ret != D_DDIM_OK) {
			// ERROR
			return ;
		}
	}

	INT32 Timer32_Utility_OneShot_Sample_Code(VOID)
	{
		ULONG		time;
		VP_CALLBACK	call_back;

		// Parameter Setting
		time      = 1000000;	// 1 sec
		call_back = 0;			// User's Callback Function None

		ret = Dd_PMC_TMR32_SetTimer(time, call_back);
		if (ret != D_DDIM_OK) {
			// ERROR
			return ;
		}
		ret = Dd_PMC_TMR32_StartOneshot();
		if (ret != D_DDIM_OK) {
			// ERROR
			return ;
		}
	}

	@endcode
*/
/*@}*//* --- end of dd_pmc_tmr32_sequence */
#endif	// CO_DDIM_UTILITY_USE

#ifndef _DD_PMC_TMR32_H_
#define _DD_PMC_TMR32_H_

/** @weakgroup dd_pmc_tmr32_definition
@{*/

#include "driver_common.h"

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
//---------------------------- driver section ----------------------------
/* Return value of 32 bit reload timer processing result */
#define D_DD_PMC_TMR32_INPUT_PARAM_ERR		(D_DD_PMC_TMR32 | D_DDIM_INPUT_PARAM_ERROR)	/**< Input Parameter Error */

/* External Trriger Max Channel Number */
#define D_DD_PMC_TMR32_EXT_TRG_CH_NUM_MAX	(6)			/**< Channel max number that can use external trigger */

#ifdef CO_DDIM_UTILITY_USE
//---------------------------- utility section ---------------------------
// Nothing Special
//---------------------------- colabo section ----------------------------
// Nothing Special
#endif

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
//---------------------------- driver section ----------------------------
// Nothing Special

#ifdef CO_DDIM_UTILITY_USE
//---------------------------- utility section ---------------------------
// Nothing Special
//---------------------------- colabo section ----------------------------
// Nothing Special
#endif

/*----------------------------------------------------------------------*/
/* Structure															*/
/*----------------------------------------------------------------------*/
//---------------------------- driver section ----------------------------
/** CSR of reload timer */
typedef union {
	ULONG		word;			/**< for ULONG access */
	struct {
		ULONG	trg_fixed0	:1;	/**< TRG is "0" fiexed ( 1 )			*/
		ULONG	cnte		:1;	/**< Timer counter Enable ( 0 or 1 )	*/
		ULONG	uf_fixed1	:1;	/**< UF is "1" fiexed ( 1 )				*/
		ULONG	inte		:1;	/**< Interrupt Enable ( 0 or 1 )		*/
		ULONG	reld		:1;	/**< Reload Enable ( 0 or 1 )			*/
		ULONG	outl		:1;	/**< Out Level ( 0 or 1 )				*/
		ULONG				:1;	/**< Reserved							*/
		ULONG				:3;	/**< Reserved							*/
		ULONG	csl			:2;	/**< Count clock select ( 0 to 3 )		*/
		ULONG	rels		:1;	/**< Reload register select ( 0 or 1 )	*/
		ULONG				:3;	/**< Reserved							*/
		ULONG				:16;/**< Reserved							*/
    } bit;						/**< for bit access						*/
} U_DD_PMC_TMR32_CSR;

/** Control information */
typedef struct {
	U_DD_PMC_TMR32_CSR	csr;			/**< Control status    TMCSR 								*/
	ULONG				rlr1;			/**< Reload register1  TMRLR1 ( 0x00000000 tor 0xFFFFFFFF )	*/
	ULONG				rlr2;			/**< Reload register2  TMRLR2 ( 0x00000000 tor 0xFFFFFFFF )	*/
	ULONG				soft_counter;	/**< soft counter ( 0x00000001 tor 0xFFFFFFFF )				*/
	VP_CALLBACK			pcallback;		/**< Call Back function pointer	*/
} T_DD_PMC_TMR32_CTRL;

#ifdef CO_DDIM_UTILITY_USE
//---------------------------- utility section ---------------------------
// Nothing Special
//---------------------------- colabo section ----------------------------
// Nothing Special
#endif

/*----------------------------------------------------------------------*/
/* Grobal Data															*/
/*----------------------------------------------------------------------*/
//---------------------------- driver section ----------------------------
// Nothing Special

#ifdef CO_DDIM_UTILITY_USE
//---------------------------- utility section ---------------------------
// Nothing Special
//---------------------------- colabo section ----------------------------
// Nothing Special
#endif

/*----------------------------------------------------------------------*/
/* Macro																*/
/*----------------------------------------------------------------------*/
//---------------------------- driver section ----------------------------
// Nothing Special

#ifdef CO_DDIM_UTILITY_USE
//---------------------------- utility section ---------------------------
// Nothing Special
//---------------------------- colabo section ----------------------------
// Nothing Special
#endif

/* @} */	// dd_pmc_tmr32_definition group

/** @weakgroup dd_pmc_tmr32_api
@{*/
/*----------------------------------------------------------------------*/
/* Function																*/
/*----------------------------------------------------------------------*/
//---------------------------- driver section ----------------------------
#ifdef __cplusplus
extern "C" {
#endif

/**
The operation condition of 32bit Reload Timer is set.<br>
Please set the callback function when the interruption is generated to "pCallBack".<br>
"soft_counter" is the one that how many times "load_value" is repeated is specified.<br>
When the number of "soft_counter" is repeated, the function registered in "pCallBack" is called. 

@param [in]    *tmr_ctrl	Timer32 Control Information
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Ctrl( T_DD_PMC_TMR32_CTRL const* const tmr_ctrl );

/**
The value of T_DD_PMC_TMR32_CTRL that corresponds at the time specified by "usec" is calculated.<br>
The unit of "usec" is a micro second.<br>
If it doesn't calculate, Input Parameter Error is returned.<br>

@param [in] 	usec			The designated time(micro second)
@param [out]   *tmr_ctrl		Timer32 Control Information
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Calculate( ULONG usec, T_DD_PMC_TMR32_CTRL* const tmr_ctrl );

/**
32bit Reload Timer(Channel) is started.

@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Start( VOID );

/**
32bit Reload Timer(Channel) is stopped.

@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Stop( VOID );

/**
32bit Reload Timer(Channel) is paused.

@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Pause( VOID );

/**
Dual Input Timer counter value is gotten.

@param [out]   *h_count	Pointer of TimerValue
@param [out]   *s_count	Pointer of Soft Counter
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Get_Counter( ULONG* const h_count, ULONG* const s_count );

/**
Value of T_DD_PMC_TMR32_CTRL is gotten.

@param [out]   *tmr_ctrl	Timer32 Control Information
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
@remarks	Because TMRLR1 and TMRLR2 are the register of Write Only, the value cannot be acquired. 
*/
extern	INT32	Dd_PMC_TMR32_Get_Ctrl( T_DD_PMC_TMR32_CTRL* const tmr_ctrl );

/**
The value of soft_counter is set.

@param [in]		soft_counter	Soft counter ( 0x00000001 to 0xFFFFFFFF )
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Set_Softcounter( ULONG soft_counter );

/**
The soft count value is gotten.

@retval soft_counter	Soft counter
*/
extern	ULONG	Dd_PMC_TMR32_Get_Softcounter( VOID );

/**
The value of reload_flg is set.<br>
If the reload_flg is "0", timer processing is not retried(=one shot).<br>
And if the reload_flg is "1", timer processing is retried(counter is reloaded when timer processing is completed).

@param [in]		flg		reload flag ( 0 to 1 )
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Set_Reload_Flg( UCHAR flg );

/**
The reload flag is gotten.

@retval gTimer32_Set_Reload_flg	The reload flag of designated ch
*/
extern	INT32	Dd_PMC_TMR32_Get_Reload_Flg( VOID );

/**
The interruption flag is cleared, and the CallBack function for 32bit Reload Timer is called.<br>
(Only an interruption clear flag of CallBack when it is unregistered.)<br>
The counter value is calculated according to specified timing.

*/
extern	VOID	Dd_PMC_TMR32_Int_Handler( VOID );

#ifdef CO_DDIM_UTILITY_USE
//---------------------------- utility section ---------------------------
/**
The operation condition of Reload Timer (Channel) is set according to designate time parameter.<br>
Please set the callback function when the interruption is generated to pCallBack.<br>
If the reload_flg is "0", timer processing is not retried(= one shot).<br>
And if the reload_flg is "1", timer processing is retried (counter is reloaded when timer processing is completed).

@param [in]		usec			Designated time(micro second)
@param [in]	  	pCallBack		Call Back function pointer
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_SetTimer( ULONG usec, VP_CALLBACK pCallBack );

/**
Designated Channel of 32bit Reload Timer is started for reload.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_StartReload( VOID );

/**
Designated Channel of 32bit Reload Timer is started for oneshot.

@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_StartOneshot( VOID );

/**
The out_level(OUTL) is set.
@param [in]		out_level		Out level ( 0 to 1 )
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TMR32_INPUT_PARAM_ERR	Input Parameter Error
*/
extern	INT32	Dd_PMC_TMR32_Set_Outlevel( UINT32 out_level );

//---------------------------- colabo section ----------------------------
// Nothing Special
#endif	// CO_DDIM_UTILITY_USE

#ifdef __cplusplus
}
#endif

/* @} */	// dd_pmc_tmr32_api group

#endif	// _DD_PMC_TMR32_H_
